package top.bbman.boot.redis.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.bbman.boot.logger.LogConstant;
import top.bbman.boot.redis.constant.PeaRedisBeanName;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * redis geo operation
 *
 * @author 祥霸
 * @version 1.0
 */
@Component(PeaRedisBeanName.DIY_GEO_REDIS)
public class PeaGeoRedis {

    @PostConstruct
    private void init() {
        LogConstant.bbManRedis.info("init geo redis");
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void set(String key, String member, double x, double y) {
        Point point = new Point(x, y);
        stringRedisTemplate.opsForGeo().add(key, point, member);
    }

    public Point get(String key, String member) {
        List<Point> list = stringRedisTemplate.opsForGeo().position(key, member);
        return CollectionUtils.isEmpty(list)? null : list.get(0);
    }
}
